
package cn.wolfcode.crm.web.controller;

import cn.wolfcode.crm.domain.Customer;
import cn.wolfcode.crm.domain.Employee;
import cn.wolfcode.crm.domain.SystemDictionaryItem;
import cn.wolfcode.crm.query.CustomerQueryObject;
import cn.wolfcode.crm.service.ICustomerService;
import cn.wolfcode.crm.service.IEmployeeService;
import cn.wolfcode.crm.service.ISystemDictionaryItemService;
import cn.wolfcode.crm.util.JsonResult;
import com.github.pagehelper.PageInfo;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.authz.annotation.Logical;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.List;

@Controller
@RequestMapping("/customer")
public class CustomerController {

    @Autowired
    private ICustomerService customerService;
    @Autowired
    private IEmployeeService employeeService;
    @Autowired
    private ISystemDictionaryItemService systemDictionaryItemService;

    @Value("job")
    private String job;

    @Value("source")
    private String source;


    @RequestMapping("/list")
    //@RequiresPermissions(value = {"客户列表权限","dept:list"},logical = Logical.OR)
    public String list(@ModelAttribute("qo") CustomerQueryObject qo, Model model) {

        //客户列表
        PageInfo<Customer> pageInfo = customerService.selectByQo(qo);
        model.addAttribute("pageInfo",pageInfo);

        //查询所有销售人员
        List<Employee> sellers = employeeService.selectEmpByRoleSns("Market");
        model.addAttribute("sellers",sellers);

        return "customer/list";
    }





    @RequestMapping("/potentialList")
    //@RequiresPermissions(value = {"潜在客户列表权限","dept:list"},logical = Logical.OR)
    public String potentialList(@ModelAttribute("qo") CustomerQueryObject qo, Model model) {
        //设置潜在客户列表的客户状态为潜在客户
        qo.setStatus(Customer.STATUS_POTENTIAL);
        //获取当前用户信息  判断是否是销售经理或超级管理员
        Employee currentEmp =(Employee)SecurityUtils.getSubject().getPrincipal();
        //不是超级管理员 或 不是市场经理
        if(!(currentEmp.isAdmin()||SecurityUtils.getSubject().hasRole("Market_Manager"))){
            //此时用户信息为普通职员 看自己的客户信息 将自己的id作为动态sql查询
            qo.setSellerId(currentEmp.getId());
        }

        PageInfo<Customer> pageInfo = customerService.selectByQo(qo);
        model.addAttribute("pageInfo", pageInfo);

        //查询所有营销人员的信息
        List<Employee> sellers = employeeService.selectEmpByRoleSns("Market");
        model.addAttribute("sellers",sellers);

        //查询所有客户的职业   根据字典明细 字典目录关联查询
        List<SystemDictionaryItem> jobs = systemDictionaryItemService.listByDictionarySn(job);
        model.addAttribute("jobs",jobs);
        // 查询所有客户的来源
        List<SystemDictionaryItem> sources = systemDictionaryItemService.listByDictionarySn(source);
        model.addAttribute("sources",sources);
        //查询交流方式
        List<SystemDictionaryItem> communicationMethod = systemDictionaryItemService.listByDictionarySn("communicationMethod");
        model.addAttribute("types",communicationMethod);


       return "customer/potentialList";
    }

    @RequestMapping("/poolList")
    //@RequiresPermissions(value = {"客户池列表权限","dept:list"},logical = Logical.OR)
    public String poolList(@ModelAttribute("qo") CustomerQueryObject qo, Model model) {
        //设置潜在客户列表的客户状态为潜在客户
        qo.setStatus(Customer.STATUS_POOL);

        PageInfo<Customer> pageInfo = customerService.selectByQo(qo);
        model.addAttribute("pageInfo", pageInfo);

        //查询所有营销人员的信息
        List<Employee> sellers = employeeService.selectEmpByRoleSns("Market");
        model.addAttribute("sellers",sellers);


       return "customer/poolList";
    }

    @RequestMapping("/failList")
    //@RequiresPermissions(value = {"客户池列表权限","dept:list"},logical = Logical.OR)
    public String failList(@ModelAttribute("qo") CustomerQueryObject qo, Model model) {
        //设置潜在客户列表的客户状态为潜在客户
        qo.setStatus(Customer.STATUS_FAILED);

        PageInfo<Customer> pageInfo = customerService.selectByQo(qo);
        model.addAttribute("pageInfo", pageInfo);

        //查询所有营销人员的信息
        List<Employee> sellers = employeeService.selectEmpByRoleSns("Market");
        model.addAttribute("sellers",sellers);

       return "customer/failList";
    }

    @RequestMapping("/normalList")
    //@RequiresPermissions(value = {"客户池列表权限","dept:list"},logical = Logical.OR)
    public String normalList(@ModelAttribute("qo") CustomerQueryObject qo, Model model) {
        //设置潜在客户列表的客户状态为潜在客户
        qo.setStatus(Customer.STATUS_NORMAL);

        PageInfo<Customer> pageInfo = customerService.selectByQo(qo);
        model.addAttribute("pageInfo", pageInfo);

        //查询所有营销人员的信息
        List<Employee> sellers = employeeService.selectEmpByRoleSns("Market");
        model.addAttribute("sellers",sellers);

       return "customer/normalList";
    }


    @RequestMapping("/saveOrUpdate")
    @ResponseBody
    //@RequiresPermissions(value = {"潜在客户编辑或新增权限","dept:saveOrUpdate"},logical = Logical.OR)
    public JsonResult saveOrUpdate(Customer customer, Model model) {
        JsonResult result = new JsonResult();
        try {
            customerService.saveOrUpdate(customer);
        } catch (Exception e) {
            e.printStackTrace();
            result.mark("保存失败");
        }
        return result;
    }

    @RequestMapping("/updateStatus")
    @ResponseBody
    @RequiresPermissions(value = {"潜在客户状态修改权限","dept:saveOrUpdate"},logical = Logical.OR)
    public JsonResult updateStatus(Long  customerId,Integer status) {
        JsonResult result = new JsonResult();
        try {
            customerService.updateStatus(customerId,status);
            //保存成功
            return result;
        } catch (Exception e) {
            e.printStackTrace();
            result.mark("保存失败");
        }
        return result;
    }




}

